﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using nyxwin.Data.Base;
using nyxwin.Data.Utils;

namespace nyxwin.Data.Bookmarks
{
	public class BookmarksManager : RemoteBase
	{
		#region Events
		public event EventHandler<GetBookmarksEventArgs> GetBookmarksCompleted;
		#endregion

		#region Construction
		public BookmarksManager(string authorizationNick, string authorizationToken)
			: base(authorizationNick, authorizationToken)
		{
			base.InvocationCompleted += new EventHandler<InvocationEventArgs>(BookmarksManagerInvocationCompleted);
		}
		#endregion

		#region GetBookmarks
		public void GetBookmarks(BookmarksTypeEnum bookmarksType)
		{
			var parameters = new Dictionary<string, string>();

			var strBookmarksType = BookmarksTypeConversion.ToString(bookmarksType);
			base.InvokeRemote("bookmarks", strBookmarksType, parameters.ToUrlArray());
		}
		#endregion

		#region BookmarksManagerInvocationCompleted
		void BookmarksManagerInvocationCompleted(object sender, InvocationEventArgs e)
		{
			if (e.Method != "bookmarks")
				throw new NyxException(string.Format("Expected 'bookmarks', but got '{0}' instead", e.Method));

			var bookmarksType = BookmarksTypeConversion.FromString(e.Parameter);
			var data = JsonConvert.DeserializeObject(e.Result) as JObject;

			var bookData = data["data"];
			if (bookData != null)
			{
				var resultList = new List<Bookmark>();

				var bookDataDiscussions = bookData["discussions"];
				var listDiscussion = new List<Bookmark>();
				foreach (var row in bookDataDiscussions)
				{
					var bookmark = new Bookmark();
					bookmark.DiscussionId = row.Value<int>("id_klub");
					bookmark.CategoryId = row.Value<int>("id_cat");
					bookmark.Title = row.Value<string>("jmeno");
					bookmark.Unread = row.Value<int>("unread");
					bookmark.Replies = row["replies"] != null ? row.Value<int>("replies") : new Nullable<int>();

					listDiscussion.Add(bookmark);
				}

				var bookDataCategories = bookData["categories"];
				foreach (var row in bookDataCategories)
				{
					var category = new Bookmark();
					category.DiscussionId = -1;
					category.CategoryId = row.Value<int>("id_cat");
					category.Title = row.Value<string>("jmeno");

					resultList.Add(category);

					foreach (var discussion in listDiscussion)
					{
						if (discussion.CategoryId == category.CategoryId)
						{
							resultList.Add(discussion);
						}
					}
				}

				FireGetBookmarksCompleted(new GetBookmarksEventArgs() { BookmarksType = bookmarksType, BookmarkList = resultList });
			}
			else
			{
				FireErrorOccurred(new ErrorOccurredEventArgs("Empty container for bookmarks"));
			}
		}
		#endregion

		#region FireGetBookmarksCompleted
		private void FireGetBookmarksCompleted(GetBookmarksEventArgs e)
		{
			if (null != GetBookmarksCompleted)
			{
				GetBookmarksCompleted(this, e);
			}
		}
		#endregion
	}
}
